<?php
class ProductsController extends AppController {
	var $name = 'Products';
	var $uses = array ('Product', 'Category','Image', 'Album', 'Brand');	
	var $helpers = array('Html', 'Form', 'Javascript', 'Tinymce', 'Time');
	var $components = array ('Upload', 'UploadImg');	
	var $layout = 'admin';
	
	
	function index()
	{
		$condition = array();
		if (!empty($this->passedArgs))
		{
			if (isset($this->passedArgs['Search.name']))
			{
				$condition[]["Product.name LIKE"] = "%{$this->passedArgs['Search.name']}%";
				$this->data['Search']['name'] = $this->passedArgs['Search.name'];
			}
			if(isset($this->passedArgs['Search.category']))
			{
				$condition[]["Category.name LIKE"] = "%{$this->passedArgs['Search.category']}%";
				$this->data['Search']['category'] = $this->passedArgs['Search.category'];
			}
		}
		
		$this->Products->recursive = 0;
		$this->paginate = array('limit' => 15, 'page' => 1, 'order' => array('Product.created_date' => 'desc'));
		$categories = $this->paginate('Product', $condition);
		$this->set('products', $categories);
	}
	function add() {
		$this->layout = 'admin';
		if (!isset($_SESSION['lang']))
    	{
    		$_SESSION['lang'] = 'vi';
    	}
		if(! empty ($this->data))
		{
			//Neu co chon anh cho san pham
			if ($this->data['Product']['images']['name'] != '')
			{
				//Upload anh, tao thumbnail va luu du lieu vao csdl
				$this->Upload->upload($this->data['Product']['images'], 'img/');
				$this->data['Product']['images'] = $this->data['Product']['images']['name'];
				$images[] =  $this->data['Product']['images']['name'];
			}   
			//Khong chon anh cho san pham
			//luu du lieu vao csdl binh thuong
			else
			{
				$this->data['Product']['images'] = 'noimage';
			}
			if($this->Product->save($this->data))
			{
				$this->Session->setFlash (__ ('Đã thêm được', true));
			}else
			{
				$this->Session->setFlash (__ ('Không thêm được', true));
			}
			
		}
		$categories =  $this->Product->findOrderList ();
		$this->set('categories', $categories);
		
		$brands = $this->Product->findBrandByOrderList();
		$this->set('brands', $brands);
	}
	function edit($id = null)
	{
		if(! $id && empty ($this->data))
		{
			$this->Session->setFlash (__ ('Invalid Page', true));
			$this->redirect (array('action' => 'index'));
		}
		if(! empty ($this->data))
		{
			
			if ($this->data['Product']['imagess']['name'])
			{
				if (!$this->Upload->upload($this->data['Product']['imagess'], WWW_ROOT . 'img' .DS))
				{
				$this->Session->setFlash('Kh™ng Upload được ảnh');
				$this->redirect($this->referer());
				}
				@unlink (WWW_ROOT . 'img'. DS . $adv['Product']['images']);
				$this->data['Product']['images'] = $this->data['Product']['imagess']['name'];
			}   
			//Khong chon anh cho san pham
			//luu du lieu vao csdl binh thuong
			else
			{
				//$this->data['Product']['images'] = 'noimage';
			}
			if($this->Product->save ($this->data))
			{
				$this->Session->setFlash (__ ('The Page has been saved', true));
				$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('The User could not be saved. Please, try again.', true));
			}
		}
		if(empty ($this->data))
		{
			$this->data = $this->Product->read (null, $id);
		}
		$categories =  $this->Product->findOrderList ();
		$this->set (compact ('categories'));
		$brands = $this->Product->findBrandByOrderList();
		$this->set('brands', $brands);
	}
	function delete($id=null)
	{
		if (!$id)
		{
			$this->Session->setFlash('Không đúng ID');
			$this->redirect ('index');
		}
		if ($this->Product->del($id))
		{
			$this->Session->setFlash('Đã xóa xong');
		}
		$this->redirect('index');
	}
	
	function processImage($images = null) {
		foreach ($images as $image)
		{
			$destinationMax = realpath(WWW_ROOT.'img'.DS) . '/';
			$destinationThumb = realpath(WWW_ROOT.'img'.DS.'thumbnail'.DS) . '/';
			$thumbmin = $this->resize($destinationMax.$image, 100, 100, $destinationThumb .$image);
		}
		$this->Session->setFlash (__ ('Sản phẩm đã được thêm', true));
		$this->redirect (array('action' => 'index'));
	}
	function dataSearch()
	{
		// the page we will redirect to
		$url['controller'] = 'products';
		$url['action'] = 'index';
		
		// build a URL will all the search elements in it
		// the resulting URL will be
		// example.com/cake/posts/index/Search.keywords:mykeyword/Search.tag_id:3
		foreach ($this->data as $k=>$v){
			foreach ($v as $kk=>$vv)
			{
				$url[$k.'.'.$kk]=$vv;
			}
		}
		
		// redirect the user to the url
		$this->redirect($url, null, true);
	}
}
?>